//
//  AlterPayPasswordTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "AlterPayPasswordTableViewController.h"
#import "BindingPhoneTableViewCell.h"
#import "LogInPassWordFooterView.h"
#import "GetPhoneNumber.h"

@interface AlterPayPasswordTableViewController ()<didClickYanZhengMaButtonDelegate,LogInPassWordFooterDelegate,UIAlertViewDelegate,UITextFieldDelegate>

{
    NSString *_phoneNumber;
}

@property (nonatomic,strong)NSArray *textFieldArray;

@property (nonatomic,strong)NSMutableArray *textFieldMutableArray;

@property (nonatomic,strong)UITextField *userTextField;

@property (nonatomic,strong)UITextField *NewPassWordTextField;

@property (nonatomic,strong)UITextField *phoneNumberTextField;

@property (nonatomic,strong)NSDictionary *userInfoDic;


@end

static NSString *const reuseIdentifier = @"reuseIdentifier";

@implementation AlterPayPasswordTableViewController

- (NSMutableArray *)textFieldMutableArray {
    if (!_textFieldMutableArray) {
        _textFieldMutableArray = [NSMutableArray array];
    }
    return _textFieldMutableArray;
}

- (NSArray *)textFieldArray {
    if (!_textFieldArray) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"AlterPayPassword" ofType:@"plist"];
        
        _textFieldArray = [NSArray arrayWithContentsOfFile:path];
        
    }
    return _textFieldArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改支付密码";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"username":USERNAME} AndMenthodName:@"api/User/info" completeBlock:^(id object) {
            if (JudgeBackDataCode) {
                self.userInfoDic = [object objectForKey:@"data"];
                NSLog(@"%@",self.userInfoDic);
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                    if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"phone"]].length) {
                        [self.tableView reloadData];
                    } else {
                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您尚未绑定手机" message:nil preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *SettingAction = [UIAlertAction actionWithTitle:@"去绑定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            [self.navigationController popViewControllerAnimated:YES];
                        }];
                        [alert addAction:SettingAction];
                        
                        [self presentViewController:alert animated:YES completion:nil];
                    }
                });
            }
        }];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    } else if (section == 1) {
        return 2;
    } else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BindingPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (!cell) {
        cell = [[BindingPhoneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if(indexPath.section == 0 && indexPath.row == 0) {
        cell.rightLabel.hidden = NO;
        cell.textField.hidden = YES;
        cell.rightLabel.text = USERNAME;
    }
    
    NSInteger place = indexPath.row + indexPath.section;
    
    if(indexPath.section == 1) {
        place ++;
        
        if (indexPath.row == 1) {
            cell.rightButton.hidden = NO;
            cell.delegate = self;
        } else {
            self.phoneNumberTextField = cell.textField;
            if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"phone"]].length) {
                self.phoneNumberTextField.text = [NSString stringWithFormat:@"%@",self.userInfoDic[@"phone"]];
            } else {
                cell.textField.text = @"未绑定";
                self.phoneNumberTextField.text = @"未绑定";
            }
            self.phoneNumberTextField.enabled = NO;
        }
        
    } else if (indexPath.section == 2) {
        cell.textField.keyboardType = UIKeyboardTypeDefault;
        cell.textField.delegate = self;
        cell.textField.secureTextEntry = YES;
        self.NewPassWordTextField = cell.textField;
        place = 4;
    }
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        cell.textField.keyboardType = UIKeyboardTypeDefault;
        cell.textField.delegate = self;
        cell.textField.secureTextEntry = YES;
        self.userTextField = cell.textField;
    }
    
    NSString *imageString = [NSString stringWithFormat:@"xiugaimima_%ld",place];
    cell.leftImageView.image = [UIImage imageNamed:imageString];
    cell.textField.placeholder = self.textFieldArray[place];
    [cell.textField addTarget:self action:@selector(didTextField:) forControlEvents:UIControlEventEditingChanged];
    cell.textField.tag = place;
    [self.textFieldMutableArray addObject:cell.textField];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    } else {
        return 10;
    }
}

- (void)didTextField:(UITextField *)textField {
    //    NSLog(@"%ld",textField.tag);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.userTextField) {
        [self.phoneNumberTextField becomeFirstResponder];
    }
    
    if (textField == self.NewPassWordTextField) {
        [self LogInPassWordFooterViewButton:nil];
    }
    return YES;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self clearTextField];
    //    NSLog(@"%ld",self.textField.tag);
}

- (void)didViewClickClearTextField {
    [self clearTextField];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self clearTextField];
}

- (void)clearTextField {
    for (UITextField *textField in self.textFieldMutableArray) {
        [textField resignFirstResponder];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return HEIGHT/5;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        LogInPassWordFooterView *footer = [[LogInPassWordFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
        footer.delegate = self;
        return footer;
    }
    return nil;
}

- (void)LogInPassWordFooterViewButton:(UIButton *)button {
    [self clearTextField];
    
    UITextField *verify = self.textFieldMutableArray[3];
    
    if (self.NewPassWordTextField.text.length != 0 && verify.text.length != 0 && self.userTextField.text.length != 0) {
        NSDictionary *params= @{
                                @"username":USERNAME,
                                //                         新密码
                                @"safe_pass":self.NewPassWordTextField.text,
                                @"verify":verify.text,
                                //                         用户密码
                                @"password": self.userTextField.text
                                };
        
        NSLog(@"%@",params);
        
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/User/setSafePass" completeBlock:^(id object) {
            
            NSLog(@"%@",object);
            
            [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"修改交易密码成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
                
                NSLog(@"%@",object);
                
                [self.navigationController popViewControllerAnimated:YES];
            }];
            
        }];
        
    } else {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确认信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        
        [self.view addSubview:hud];
        
    }
    
}

- (void)didClickYanZhengMaButton:(UIButton *)button {
    
    UITextField *phoneNumber = self.textFieldMutableArray[2];
    
    //    [[GetPhoneNumber SharedGetPhoneNumber] getPhoneVerifyNumber:button andPhoneNumber:phoneNumber.text andView:self.view];
    
    [[GetPhoneNumber SharedGetPhoneNumber] getFotgetPhoneVerifyNumber:button andPhoneNumber:phoneNumber.text andView:self.view andGetVerifyType:@"4"
     ];
    
}

- (NSDictionary *)userInfoDic {
    if (!_userInfoDic) {
        _userInfoDic = [NSDictionary dictionary];
    }
    return _userInfoDic;
}
@end
